package org.example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author zhl
 * @version 1.0
 * @ClassName
 * @date 2020/11/2 17:43
 */
public class FileExists {

    public static void main(String[] args) throws URISyntaxException, IOException {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        String HDFSUri = "hdfs://mnode1:9000";

        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI(HDFSUri), conf);

        String filename = "test";
        checkFileExists(filename, fs);
        filename = "input/core-site.xml";
        checkFileExists(filename, fs);
        fs.close();

    }

    /**
     * 判断文件在hdfs系统是否存在
     * */
    public static void checkFileExists(String filename, FileSystem fileSystem) throws IOException {
        if (fileSystem.exists(new Path(filename))){
            System.out.println("文件存在");
        }else {
            System.out.println("文件不存在");
        }
    }
}
